package com.lzq.course.server.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lzq.course.server.domain.Member;
import com.lzq.course.server.domain.MemberExample;
import com.lzq.course.server.dto.LoginMemberDto;
import com.lzq.course.server.dto.MemberDto;
import com.lzq.course.server.dto.PageDto;
import com.lzq.course.server.exception.BusinessException;
import com.lzq.course.server.exception.BusinessExceptionCode;
import com.lzq.course.server.mapper.MemberMapper;
import com.lzq.course.server.util.CopyUtil;
import com.lzq.course.server.util.UuidUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Date;

@Service
public class MemberService {
    private final static Logger LOG = LoggerFactory.getLogger(MemberService.class);
    @Resource
    private MemberMapper memberMapper;

    /**
     * example 这个文件很有用
     * 可以帮我们生成where order 查询
     * 不用单独写sql的增删改查
     */
    public void list(PageDto<MemberDto> pageDto) {
        // 分页查询 页码不是从0开始
        PageHelper.startPage(pageDto.getPage(), pageDto.getSize());

        // 排序
        MemberExample memberExample = new MemberExample();

        // 查询
        List<Member> memberList = memberMapper.selectByExample(memberExample);

        PageInfo<Member> pageInfo = new PageInfo<>(memberList);
        pageDto.setTotal(pageInfo.getTotal());

        List<MemberDto> memberDtoList = CopyUtil.copyList(memberList, MemberDto.class);

        pageDto.setList(memberDtoList);
    }

    /**
     * 保存 修改
     * @param memberDto
     */
    public void save(MemberDto memberDto) {
        //BeanUtils.copyProperties(memberDto, member);
        Member copyMember = CopyUtil.copy(memberDto, Member.class);

        if (StringUtils.isEmpty(memberDto.getId())) {
            // 新增
            // 八位的 id
            copyMember.setId(UuidUtil.getShortUuid());

            Date now = new Date();

            memberMapper.insert(copyMember);
        } else {
            // 编辑
            memberMapper.updateByPrimaryKey(copyMember);
        }
    }

    /**
     * 删除
     * @param id
     */
    public void delete(String id) {
        memberMapper.deleteByPrimaryKey(id);
    }

    /**
     * 登录
     * @param memberDto
     */
    public LoginMemberDto login(MemberDto memberDto) {
        // 校验用户名秘密
        Member member = selectByMobile(memberDto.getMobile());
        if (member == null) {
            LOG.info("手机号不存在, {}", memberDto.getMobile());
            throw new BusinessException(BusinessExceptionCode.LOGIN_MEMBER_ERROR);
        } else {
            // 密码正确
            if (memberDto.getPassword().equals(member.getPassword())) {
                return CopyUtil.copy(member, LoginMemberDto.class);
            } else {
                // 密码错误
                throw new BusinessException(BusinessExceptionCode.USER_LOGIN_ERROR);
            }
        }
    }

    /**
     * 按手机号查找
     * @param mobile
     * @return
     */
    public Member selectByMobile(String mobile) {
        if (StringUtils.isEmpty(mobile)) {
            return null;
        }
        MemberExample example = new MemberExample();
        example.createCriteria().andMobileEqualTo(mobile);
        List<Member> memberList = memberMapper.selectByExample(example);
        if (memberList == null || memberList.size() == 0) {
            return null;
        } else {
            return memberList.get(0);
        }

    }

    /**
     * 判断手机号是否存在
     * @param mobile
     * @return true 手机号已存在
     */
    public boolean findMobile(String mobile) {
        Member member = this.selectByMobile(mobile);
        return member != null;
    }
}
